import { getPage } from '../../utils/config';
import api from '../../utils/api';
import { http, extend, $toast, $loading } from '../../utils/utils';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        getPage: getPage,
        tabsActive: 0,
        loading: 0,
        pageSize: 10,
        loadmore: false,
        goodsId: 0,
    },

    /**
     * 切换显示
     */
    tabsChange(e){
        const { index } = e.detail;
        let { commentsInfo } = this.data;
        
        this.setData({ tabsActive: index });

        if (!commentsInfo[index].load) {
            this.setData({ loading: 0 });
            this.getCommentInfo((res) => {
                commentsInfo[index].list = res.list;
                this.setData({ commentsInfo, loading: 1 });
            });
        }
    },

    /**
     * 获取用户全部评论 -- 网路请求
     */
    getCommentInfo(cb, a = false) {
        const { tabsActive, goodsId, pageSize, commentsInfo  } = this.data;
        http.post(api.default, {
            data: {
                action: 'getgoodscomment',
                pageindex: commentsInfo[tabsActive].page,
                pagesize: pageSize,
                id: goodsId,
                typeid: tabsActive ? 2 : 1,
            }
        }, a).then((res) => {
            const { data } = res.data;

            //判断是否已加载
            if (!commentsInfo[tabsActive].load) {
                commentsInfo[tabsActive].load = true;
            }

            //判断是否没有数据
            if (data.list.length == 0 && commentsInfo[tabsActive].page == 1) {
                commentsInfo[tabsActive].abnor = true;
            } else {
                for (let i = 0; i < data.list.length; i++) {
                    if (data.list[i].pic) {
                        data.list[i].pic = data.list[i].pic.split(',');
                    }
                    if (data.list[i].content_sec_pic) {
                        data.list[i].content_sec_pic = data.list[i].content_sec_pic.split(',');
                    }
                }
            }

            //判断是否还有下一页
            if (data.list.length < pageSize) {
                commentsInfo[tabsActive].nextPages = false;
            }

            this.setData({ commentsInfo });

            cb && cb(data); 
        })
    },

    /**
     * 预览图片
     */
    picViewTap(e) {
        const { imageCdn } = api;
        const { tabsActive, commentsInfo } = this.data;
        let { src, parentIndex, mode } = e.currentTarget.dataset || e.target.dataset;
        let urls = [],
            picList = [],
            lists = commentsInfo[tabsActive].list[parentIndex];

        picList = Number(mode) ? lists.content_sec_pic : lists.pic;
        
        picList.forEach((item) => {
            urls.push(imageCdn + item);
        });

        wx.previewImage({
            current: imageCdn + src,
            urls: urls
        })
    },

    /**
     * 下拉刷新
     */
    handleToUpper() {
        let { tabsActive, commentsInfo } = this.data;
        
        $loading();

        //恢复默认请求参数
        commentsInfo[tabsActive] = extend({}, commentsInfo[tabsActive], {
            page: 1,
            abnor: false,
            load: false,
            nextPages: true,
        });

        this.setData({ commentsInfo });

        this.getCommentInfo((res) => {
            commentsInfo[tabsActive].list = res.list;
            this.setData({ commentsInfo });
            setTimeout(() => {
                this[`scroll${tabsActive}`].stop();
                wx.hideLoading();
            }, 1000)
        });
    },

    /**
     * 上拉加载
     */
    handleTolower() {
        let { tabsActive, loadmore, commentsInfo } = this.data;

        if (commentsInfo[tabsActive].nextPages && !loadmore) {
            commentsInfo[tabsActive].page++;
            this.setData({ commentsInfo, loadmore: true });

            this.getCommentInfo((res) => {
                commentsInfo[tabsActive].list = commentsInfo[tabsActive].list.concat(res.list);
                this.setData({ commentsInfo, loadmore: false });
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        //携带参数
        if(JSON.stringify(options) != '{}'){
            this.setData({ goodsId: options.id });
        }

        const { tabsActive } = this.data;
        let commentsInfo = [];

        [0, 1].forEach((item) => {
            commentsInfo.push({
                page: 1,
                abnor: false,
                load: false,
                list: [],
                nextPages: true,
            });
        });

        this.setData({ commentsInfo });

        this.getCommentInfo((res) => {
            commentsInfo[tabsActive].list = res.list;
            this.setData({ commentsInfo, loading: 1 });
        });

        [0, 1].forEach((item) => {
            this[`scroll${item}`] = this.selectComponent(`#scroll-${item}`);
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})